<?php
require_once('../qcubed/includes/configuration/prepend.inc.php');
require_once('../build/includes/config.php');
require_once('lookups.php');
require_once('../mantis/core.php');


auth_ensure_user_authenticated();

$t_current_user_id = ((isset($_SESSION["schedfilteruser"])) ? intval($_SESSION["schedfilteruser"]) : auth_get_current_user_id());

Header("Content-type: text/xml");

$filter = ((isset($_SESSION["schedfilter"])) ? $_SESSION["schedfilter"] : "proj");

if (isset($_SESSION["activeprojectid"])) {
	$projectid = intval($_SESSION["activeprojectid"]);
} else {
	if ($filter == $proj) $filter = "projall";
}

$mine = false;
switch ($filter) {
case "proj" :
	$projects = array(MantisProjectTable::LoadById($projectid));
	break;
case "all" :
	$projects = MantisProjectTable::LoadAll();
	break;
case "projmine" :
	$projects = array(MantisProjectTable::LoadById($projectid));
	$mine = true;
	break;
case "allmine" :
	$projects = MantisProjectTable::LoadAll();
	$mine = true;
	break;
}


?><<?php echo '?'; ?>xml version="1.0" encoding="UTF-8"?>
<projects>
<?php
$name = "";
for ($i=0; $i<count($projects); $i++) {

//	$bugs = MantisBugTable::QueryArray(QQ::AndCondition(QQ::Equal(QQN::MantisBugTable()->ProjectId, $projects[$i]->Id), QQ::LessThan(QQN::MantisBugTable()->Status, 50)));
	if (!$mine) {
		$bugs = MantisBugTable::QueryArray(QQ::AndCondition(QQ::Equal(QQN::MantisBugTable()->ProjectId, $projects[$i]->Id), QQ::LessThan(QQN::MantisBugTable()->Status, 60)));
	} else {
		$bugs = MantisBugTable::QueryArray(QQ::AndCondition(
			QQ::Equal(QQN::MantisBugTable()->ProjectId, $projects[$i]->Id), 
			QQ::LessThan(QQN::MantisBugTable()->Status, 60),
			QQ::Equal(QQN::MantisBugTable()->HandlerId, $t_current_user_id)
				));
	}

	if (count($bugs) == 0) continue;
	$projStartDate = $bugs[0]->StartDate;
	?>
  <project id="<?php echo $projects[$i]->Id; ?>" name="<?php echo $projects[$i]->Name; ?>" startdate="<?php echo $projStartDate->Format('Y'); ?>,<?php echo $projStartDate->Format('m'); ?>,<?php echo $projStartDate->Format('d'); ?>">
<?php 
	
	for ($j=0; $j<count($bugs); $j++) {
		$startDate = $bugs[$j]->StartDate;
				//die("??" . $startDate->IsEarlierThan($projStartDate) . "??");
		if ($startDate->IsEarlierThan($projStartDate)) {
			$startDate = $projStartDate;	
		}
		
		try {
			$diff = new QDateTimeSpan();
			$diff->SetFromQDateTime($bugs[$j]->EndDate, $startDate);
			$duration = ($diff->Days * 8) * (($diff->Days < 0) ? -1 : 1);
		} catch (ErrorException $e) {
			$duration = 24;	
		}
		
		if ($bugs[$j]->HandlerId > 0) {
			$user = MantisUserTable::LoadById($bugs[$j]->HandlerId);
			$shortdesc = $user->Realname;
		} else {
			$shortdesc = "unassigned";	
		}
		
		if ($bugs[$j]->TargetVersion > 0) {
			$version = MantisProjectVersionTable::LoadById($bugs[$j]->TargetVersion);
			
			$shortdesc = $shortdesc . " - " . $version->Version;
		}
		?>
    <task id="<?php echo $bugs[$j]->Id; ?>">
  	<name><?php echo $bugs[$j]->Summary . " (" . $shortdesc . ")"; ?></name>
	<est><?php echo $startDate->Format('Y'); ?>,<?php echo $startDate->Format('m'); ?>,<?php echo $startDate->Format('d'); ?></est>
	<duration><?php echo $duration; ?></duration>
    	<percentcompleted>100</percentcompleted>
		<predecessortasks></predecessortasks>
            <childtasks></childtasks>
            <!--childtasks>
                  <task id="2">
                    <name>project1 task2</name>
              	    <est>2006,12,14</est>
        	    <duration>100</duration>
    	            <percentcompleted>20</percentcompleted>
        	    <predecessortasks></predecessortasks>
                    <childtasks></childtasks>
        	  </task>
                  <task id="6">
            	    <name>project1 task6</name>
	            <est>2006,12,15</est>
            	    <duration>90</duration>
        	    <percentcompleted>10</percentcompleted>
        	    <predecessortasks></predecessortasks>
                    <childtasks></childtasks>
                  </task>
            </childtasks-->
     </task>
     <?php
	}
?>
  </project>
<?php
}
?>
</projects>